﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CarBoard
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        static double total_trip_start= 1800;
        private double single_trip;
        private double total_trip;
        //代理类
        public class DelegateCollection
        {
            public delegate void updateUIContorl(string txt);
        }
        //数据模拟函数
        private void DataControl(int timegap)
        {
            try
            {
                while (true)
                {
                    Random ran = new Random();
                    //水温
                    double water = ran.Next(45, 55) + ran.NextDouble();
                    this.water.Dispatcher.Invoke(new Action(delegate
                    {
                        this.water.Value = water;
                    }));

                    //车速
                    double speed = ran.Next(0, 120) + ran.NextDouble();
                    this.speed.Dispatcher.Invoke(new Action(delegate
                    {
                        this.speed.Value = speed;
                    }));

                    //单次里程
                    single_trip += ran.Next(1, 3) * 0.5;
                    this.trip.Dispatcher.Invoke(new Action(delegate
                    {
                        this.trip.Value = single_trip;
                    }));
                    //总里程
                    total_trip = total_trip_start + single_trip;
                    this.totaltrip.Dispatcher.Invoke(new Action(delegate
                    {
                        this.totaltrip.Value = total_trip;
                    }));
                    //电量
                    double dec_bat = 2;
                    this.battery.Dispatcher.Invoke(new Action(delegate
                    {
                        if (this.battery.Value > 0)
                        {
                            this.battery.Value -= dec_bat;
                        }
                        else
                        {
                            this.battery.Value = 0;
                        }
                    }));
                    this.leftpower.Dispatcher.Invoke(new Action(delegate
                    {
                        if (this.leftpower.Value > 0)
                        {
                            this.leftpower.Value -= dec_bat;
                        }
                        else
                        {
                            this.leftpower.Value = 0;
                        }
                    }));
                    
                    //车辆避碰
                    double lf = ran.Next(1, 4) + ran.NextDouble();
                    double rf = ran.Next(1, 4) + ran.NextDouble();
                    double lb = ran.Next(1, 4) + ran.NextDouble();
                    double rb = ran.Next(1, 4) + ran.NextDouble();
                    this.lf.Dispatcher.Invoke(new Action(delegate
                    {
                        this.lf.Value = lf;
                    }));
                    this.rf.Dispatcher.Invoke(new Action(delegate
                    {
                        this.rf.Value = rf;
                    }));
                    this.lb.Dispatcher.Invoke(new Action(delegate
                    {
                        this.lb.Value = lb;
                    }));
                    this.rb.Dispatcher.Invoke(new Action(delegate
                    {
                        this.rb.Value = rb;
                    }));

                    //车内温度
                    double temper = ran.Next(15,22) + ran.NextDouble();
                    this.temper.Dispatcher.Invoke(new Action(delegate
                    {
                        this.temper.Value = temper;
                    }));
                    //功率
                    double rate = ran.Next(50, 60) + ran.NextDouble();
                    this.powerrate.Dispatcher.Invoke(new Action(delegate
                    {
                        this.powerrate.Value = rate;
                    }));
                    //电流
                    double current = ran.Next(15, 22) + ran.NextDouble();
                    this.current.Dispatcher.Invoke(new Action(delegate
                    {
                        this.current.Value = current;
                    }));
                    //平均耗电量
                    double avCons = ran.Next(35, 39) + ran.NextDouble();
                    this.avCons.Dispatcher.Invoke(new Action(delegate
                    {
                        this.avCons.Value = avCons;
                    }));

                    //实际时间
                    string date = DateTime.Now.ToString("yyyy年MM月dd日 ddd");
                    string time = DateTime.Now.ToString("hh：mm：ss t\\M");
                    this.date.Dispatcher.Invoke(new Action(delegate
                    {
                        this.date.Content = date;
                    }));
                    this.time.Dispatcher.Invoke(new Action(delegate
                    {
                        this.time.Content = time;
                    }));

                    //电压
                    double voltage = ran.Next(80,120) + ran.NextDouble();
                    this.voltage.Dispatcher.Invoke(new Action(delegate
                    {
                        this.voltage.Value = voltage;
                    }));

                    Thread.Sleep(timegap);
                }
            }
            catch
            {

            }
        }

        public MainWindow()
        {
            InitializeComponent();
            //线程刷新时间(ms)
            const int timegap = 500;
            single_trip = 0;
            total_trip = total_trip_start;
            //执行数据模拟
            Thread thread = new Thread(new ThreadStart(new Action(() =>
            {
                DataControl(timegap);
            })));
            thread.Start();
        }
    }
}
